//
//  ZXStatuses.m
//  ZXOCWeiBo
//
//  Created by mac on 15/12/4.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXStatuses.h"
#import "ZXPhoto.h"
@implementation ZXStatuses
-(NSString *)created_at{
    //created_at get方法把原来时间进行相应的转化
    /*今年
     今天
     1分之内 ："刚刚"
     1分～60分内之间 : "几分钟前"
     1时~24小时之内："几小时前"
     昨天
     昨天 HH:mm "昨天 12:23"
     昨天以前
     几天前 n 天前
     去年
     2014 MM-dd 2014 05-14
     */
    
    //Thu May 14 10:50:49 +0800 2015
    
    //    _created_at = @"Thu May 12 10:50:49 +0800 2014";
    // 1.把字符串的时间转成NSDate
    NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
    dateForm.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    
#warning 时间转化的时候，设置NSDateFormatter的local 真上转化时间的时候会为nil;
    dateForm.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    NSDate *fromDate = [dateForm dateFromString:_created_at];
    
    NSString *tmpDateStr = @"";
    
    // 计算_created_at 与 当前时间差
    NSDateComponents *components = [fromDate deltaWithNow];
    if (fromDate.isThisYear) {//今年
        if (fromDate.isToday) {//今天
            if (components.hour >= 1) {//几小时前
                tmpDateStr = [NSString stringWithFormat:@"%ld小时前",components.hour];
            }else if(components.minute >= 1){//"几分钟前"
                tmpDateStr = [NSString stringWithFormat:@"%ld分钟前",components.minute];
            }else{//刚刚
                tmpDateStr = @"刚刚";
            }
        }else if(fromDate.isYesterday){//昨天
            dateForm.dateFormat = @"HH:mm";
            NSString *hhmm = [dateForm stringFromDate:fromDate];
            tmpDateStr = [NSString stringWithFormat:@"昨天 %@",hhmm];
        }else{// 几天前
            //            CZLog(@"AAA %ld",components.day);
            tmpDateStr = [NSString stringWithFormat:@"%ld天前",components.day];
        }
        
    }else{//去年以前
        dateForm.dateFormat = @"yyyy MM-dd";
        tmpDateStr = [dateForm stringFromDate:fromDate];
    }
    
    
    
    
    return tmpDateStr;
}

-(void)test:(NSDate *)from to:(NSDate *)to{
    //日历类,可以计算两个时间差
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    
    /* NSCalendarUnitYear
     NSCalendarUnitMonth
     NSCalendarUnitDay
     NSCalendarUnitHour
     NSCalendarUnitMinute
     NSCalendarUnitSecond*/
    
    NSCalendarUnit unit = NSCalendarUnitSecond |NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitYear;
    //
    //    NSDate *currentDate = [NSDate date];
    //    NSDate *toDate = [currentDate dateByAddingTimeInterval:60 * 60 * 24 * 366 + 59];
    
    NSDateComponents * components = [calendar components:unit fromDate:from toDate:to options:NSCalendarWrapComponents];
    
    NSLog(@"年数相差: %ld",components.year);
    NSLog(@"天数相差: %ld",components.day);
    NSLog(@"小时数相差: %ld",components.hour);
    NSLog(@"分钟数相差: %ld",components.minute);
    NSLog(@"秒数相差: %ld",components.second);
}

+(NSDictionary *)objectClassInArray
{
    return @{@"pic_urls":[ZXPhoto class]};

}

-(void)setSource:(NSString *)source
{

    ZXLog(@"-----%@",source);
    NSRange range = [source rangeOfString:@">"];
    
    NSString * tmpStr = [source substringFromIndex:range.location + 1];
    
    range = [tmpStr rangeOfString:@"<"];
    
    tmpStr = [tmpStr substringToIndex:range.location];
    ZXLog(@"=====%@",tmpStr);
    
    if (tmpStr==nil) {
        return;
    }
    
    _source = [tmpStr copy];
}

@end
